
function rankStudent(R0, x, w = [0.25, 0.25, 0.25, 0.25]) {
  const n = R0.length; // 指标数
  const m = x.length; // 老师数

  // 计算关联系数矩阵
  const R = new Array(n);
  for (let i = 0; i < n; i++) {
    R[i] = new Array(n);
    for (let j = 0; j < n; j++) {
      const rMax = Math.max(...R0[i]);
      const rMin = Math.min(...R0[j]);
      R[i][j] = (rMax - rMin) / (rMax - rMin + Math.abs(R0[i][0] - R0[j][0]));
    }
  }

  // 计算加权得分矩阵
  const v = new Array(m);
  for (let i = 0; i < m; i++) {
    v[i] = new Array(n);
    for (let j = 0; j < n; j++) {
      let sum = 0;
      for (let k = 0; k < R0[j].length; k += 2) {
        if (x[i][j] >= R0[j][k] && x[i][j] <= R0[j][k+1]) {
          const rank = k / 2 + 1;
          sum += rank * R0[j][k+1] - (rank - 1) * R0[j][k];
          break;
        }
      }
      v[i][j] = sum / (R0[j].length / 2);
    }
  }

  // 计算综合得分矩阵
  const s = new Array(m);
  for (let i = 0; i < m; i++) {
    s[i] = 0;
    for (let j = 0; j < n; j++) {
      s[i] += v[i][j] * w[j];
    }
  }

  // 计算加权核心度矩阵
  const P = new Array(n);
  for (let i = 0; i < n; i++) {
    P[i] = 0;
    for (let j = 0; j < n; j++) {
      P[i] += R[i][j] * w[j];
    }
  }

  // 计算综合核心度
  const Q = P.reduce((acc, cur) => acc + cur);

  // 计算评价等级
  const rankMap = {
    '优': [0.85, 1],
    '良': [0.75, 0.85],
    '中': [0.6, 0.75],
    '差': [0, 0.6]
  };
  const score = s.reduce((acc, cur) => acc + cur) / m;
  for (const key in rankMap) {
    const [low, high] = rankMap[key];
    if (score >= Q * low && score <= Q * high) {
      return key;
    }
  }
}
const R0 = [
    [0, 10, 11, 20, 21, 30, 31, 40],
    [0, 10, 11, 20, 21, 30, 31, 40],
    [0, 10, 11, 20, 21, 30, 31, 40],
    [0, 10, 11, 20, 21, 30, 31, 40],
];

const x = [
    [40, 40, 40, 40],
    [40, 40, 40, 40],
    [40, 40, 40, 40],
    [40, 40, 40, 40]
];
console.log(rankStudent(R0, x), '结果')
